import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Color;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JTabbedPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import net.miginfocom.swing.MigLayout;


public class Raumverwaltung extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JTextField txtRaumname;
	private JTextField txtAnzReihenParkett;
	private JTextField txtAnzSitzeParkett;
	private JTextField txtAnzReihenLoge;
	private JTextField txtAnzSitzeLoge;
	private String raumname;
	private int anzreihenparkett,anzsitzeparkett,anzreihenloge,anzsitzeloge;
	private JTable jTable1 = new JTable(30, 6);
    private DefaultTableModel jTable1Model = (DefaultTableModel) jTable1.getModel();
    private JScrollPane jTable1ScrollPane = new JScrollPane(jTable1);
    private Database db;
    private User user;


	public Raumverwaltung(Database db, User userObj) throws SQLException {
		this.db=db;
		this.user=userObj;
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 870, 620);
	    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
	    int x = (d.width - getSize().width) / 2;
	    int y = (d.height - getSize().height) / 2;
	    setLocation(x, y);
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenuItem mntmKinoverwaltung = new JMenuItem("Kinoverwaltung");
		mntmKinoverwaltung.setFont(new Font("Verdana", Font.PLAIN, 14));
		mntmKinoverwaltung.addActionListener(this);

		menuBar.add(mntmKinoverwaltung);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(51, 153, 204));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblTakeASeat = new JLabel("Take A Seat - Raumverwaltung");
		lblTakeASeat.setForeground(Color.WHITE);
		lblTakeASeat.setFont(new Font("Verdana", Font.BOLD, 36));
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setFont(new Font("Verdana", Font.PLAIN, 14));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblTakeASeat, GroupLayout.PREFERRED_SIZE, 834, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(61, Short.MAX_VALUE))
				.addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE, 905, Short.MAX_VALUE)
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblTakeASeat, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE, 661, Short.MAX_VALUE))
		);
		
		JPanel panel = new JPanel();
		tabbedPane.addTab("Raum anlegen", null, panel, null);
		
		JLabel label_raumname = new JLabel("Raumname");
		label_raumname.setFont(new Font("Verdana", Font.BOLD, 14));
		
		JLabel lblAnzahlReihenParkett = new JLabel("Anzahl Reihen Parkett");
		lblAnzahlReihenParkett.setFont(new Font("Verdana", Font.BOLD, 14));
		
		JLabel lblAnzahlSitzeParkett = new JLabel("Anzahl Sitze Parkett");
		lblAnzahlSitzeParkett.setFont(new Font("Verdana", Font.BOLD, 14));
		
		JLabel lblAnzahlReihenLoge = new JLabel("Anzahl Reihen Loge");
		lblAnzahlReihenLoge.setFont(new Font("Verdana", Font.BOLD, 14));
		
		JLabel lblAnzahlSitzeLoge = new JLabel("Anzahl Sitze Loge");
		lblAnzahlSitzeLoge.setFont(new Font("Verdana", Font.BOLD, 14));
		
		txtRaumname = new JTextField();
		txtRaumname.setFont(new Font("Verdana", Font.PLAIN, 12));
		txtRaumname.setColumns(10);
		
		txtAnzReihenParkett = new JTextField();
		txtAnzReihenParkett.setFont(new Font("Verdana", Font.PLAIN, 12));
		txtAnzReihenParkett.setColumns(10);
		
		txtAnzSitzeParkett = new JTextField();
		txtAnzSitzeParkett.setFont(new Font("Verdana", Font.PLAIN, 12));
		txtAnzSitzeParkett.setColumns(10);
		
		txtAnzReihenLoge = new JTextField();
		txtAnzReihenLoge.setFont(new Font("Verdana", Font.PLAIN, 12));
		txtAnzReihenLoge.setColumns(10);
		
		txtAnzSitzeLoge = new JTextField();
		txtAnzSitzeLoge.setFont(new Font("Verdana", Font.PLAIN, 12));
		txtAnzSitzeLoge.setColumns(10);
		
		JButton btnRaumErstellen = new JButton("Raum erstellen");
		btnRaumErstellen.addActionListener(this);
		btnRaumErstellen.setFont(new Font("Verdana", Font.PLAIN, 14));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(22)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
							.addComponent(label_raumname, GroupLayout.PREFERRED_SIZE, 125, GroupLayout.PREFERRED_SIZE)
							.addComponent(lblAnzahlSitzeParkett, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(lblAnzahlReihenParkett))
						.addComponent(lblAnzahlReihenLoge, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblAnzahlSitzeLoge, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE))
					.addGap(29)
					.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
						.addComponent(btnRaumErstellen, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(txtAnzSitzeLoge, Alignment.LEADING)
						.addComponent(txtAnzReihenLoge, Alignment.LEADING)
						.addComponent(txtAnzSitzeParkett, Alignment.LEADING)
						.addComponent(txtAnzReihenParkett, Alignment.LEADING)
						.addComponent(txtRaumname, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE))
					.addContainerGap(369, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(36)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_raumname, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtRaumname, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(25)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblAnzahlReihenParkett, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtAnzReihenParkett, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(32)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblAnzahlSitzeParkett, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtAnzSitzeParkett, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(35)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblAnzahlReihenLoge, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtAnzReihenLoge, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(32)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblAnzahlSitzeLoge, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtAnzSitzeLoge, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(32)
					.addComponent(btnRaumErstellen, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(81, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("Raum \u00E4ndern", null, panel_1, null);
		
		jTable1.getColumnModel().getColumn(0).setHeaderValue("ID");
		jTable1.getColumnModel().getColumn(1).setHeaderValue("Raumname");
		jTable1.getColumnModel().getColumn(2).setHeaderValue("Anzahl Sitze Parkett");
		jTable1.getColumnModel().getColumn(3).setHeaderValue("Anzahl Reihen Parkett");
		jTable1.getColumnModel().getColumn(4).setHeaderValue("Anzahl Sitze Loge");
		jTable1.getColumnModel().getColumn(5).setHeaderValue("Anzahl Reihen Loge");
		
		
		 ResultSet rs = db.getData("SELECT * FROM raum");
		    
		    int row=0;
		    	    
		    while(rs.next()){
		    	    jTable1.setValueAt(rs.getString(1),row,0);
		    	    jTable1.setValueAt(rs.getString(2),row,1);
		    	    jTable1.setValueAt(rs.getString(3),row,2);
		    	    jTable1.setValueAt(rs.getString(4),row,3);
		    	    jTable1.setValueAt(rs.getString(5),row,4);
		    	    jTable1.setValueAt(rs.getString(6),row,5);
		    	   // System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4));
		    	    row++;
		    }
		
		JButton btnnderungenSpeichern = new JButton("\u00C4ndern");
		btnnderungenSpeichern.setFont(new Font("Verdana", Font.PLAIN, 14));
		btnnderungenSpeichern.addActionListener(this);
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addComponent(jTable1ScrollPane, GroupLayout.PREFERRED_SIZE, 629, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnnderungenSpeichern, GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGap(18)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
						.addComponent(btnnderungenSpeichern, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
						.addComponent(jTable1ScrollPane, GroupLayout.PREFERRED_SIZE, 417, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(16, Short.MAX_VALUE))
		);
		panel_1.setLayout(gl_panel_1);
		contentPane.setLayout(gl_contentPane);
		
		setVisible(true);
	}
	
	@Override
	public void actionPerformed(ActionEvent e){
		
		if (e.getActionCommand()=="Kinoverwaltung"){
			dispose();
			try {
				new Kinoverwaltung(db, user);//.main(null);
			} catch (NumberFormatException | SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
		if(e.getActionCommand()=="\u00C4ndern"){
			System.out.println("ActionPerformed angelangt Event korrekt");
			int id = Integer.parseInt((String) jTable1.getValueAt(jTable1.getSelectedRow(), 0));
			try {
				new RaumChange(id, db, user);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			dispose();
		}
		
		if (e.getActionCommand()=="Raum erstellen"){
		raumname=txtRaumname.getText();
		anzreihenparkett=Integer.parseInt(txtAnzReihenParkett.getText()) ;
		anzsitzeparkett=Integer.parseInt(txtAnzSitzeParkett.getText()) ;
		anzreihenloge=Integer.parseInt(txtAnzReihenLoge.getText()) ;
		anzsitzeloge=Integer.parseInt(txtAnzSitzeLoge.getText()) ;
		
	    try {
			db.setRoom(raumname, anzreihenparkett, anzsitzeparkett, anzreihenloge,anzsitzeloge,db.getNextID(4));
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		}
		
	}
}
